#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    using LL = long long;
    int countOk(vector<LL>& prev, int end, LL k)
    {
        int left = 0, right = end;
        LL t = prev[end];
        while (left <= right)
        {
            int mid = (right - left) / 2 + left;
            if ((t - prev[mid]) * (end - mid) >= k)
                left = mid + 1;
            else
                right = mid - 1;
        }
        return end - left;
    }
    long long countSubarrays(vector<int>& nums, long long k) {
        int n = nums.size();
        vector<LL> prev(n + 1, 0);
        LL res = 0;
        for (int i = 1; i <= n; i++)
        {
            prev[i] = prev[i - 1] + nums[i - 1];
            res += countOk(prev, i, k);
        }
        return res;
    }
};